
package com.kugou.game.sdk.statistics;

import com.kugou.framework.http.RequestPackage;
import com.kugou.framework.utils.UrlEncodeUtil;
import com.kugou.game.sdk.core.SDKControler;
import com.kugou.game.sdk.utils.AppUtil;
import com.kugou.game.sdk.utils.UrlWrapper;

import android.content.Context;

import java.util.HashMap;
import java.util.Set;

/**
 * 描述:H5 游戏中心下载统计(下载完成、下载出错)
 * 
 * @author liux
 * @since 2014-11-17 下午03:09:26
 */
public class StatGameCenterDownloadTask extends AbsStatisticsTask {

    /** 统计项定义：下载完成无暂停 */
    public static final int ITEM_DOWNLOAD_COMPLETE_NO_PAUSE = 16;

    /** 统计项定义：下载完成有暂停 */
    public static final int ITEM_DOWNLOAD_COMPLETE_WITH_PAUSE = 17;

    /** 统计项定义：下载中出错 */
    public static final int ITEM_DOWNLOAD_ERROR = 18;

    private Context mContext;

    private String mPositionid;

    private int mItemid;

    private int mErrorcode;

    private String mStatvalue;

    private String mErrorlog;

    private String mAppid;

    private String mMid;

    private String mUuid;

    private String mKugouid;

    private String mKugoutoken;

    private long mClientAppid;

    private String mStatParams;

    private HashMap<String, String> mParams = new HashMap<String, String>();

    public StatGameCenterDownloadTask(Context context) {
        super(context);
    }

    public StatGameCenterDownloadTask(Context context, String gameid, String positionid, int itemid,
            String statvalue, int errorcode, String errorlog, String mid, String uuid,
            String kugouid, String kugoutoken, long clientAppid, String statParams) {
        super(context);
        this.mContext = context;
        this.mAppid = gameid;
        this.mPositionid = positionid;
        this.mItemid = itemid;
        this.mStatvalue = statvalue;
        this.mErrorcode = errorcode;
        this.mErrorlog = errorlog;
        this.mMid = mid;
        this.mUuid = uuid;
        this.mKugouid = kugouid;
        this.mKugoutoken = kugoutoken;
        this.mClientAppid = clientAppid;
        this.mStatParams = statParams;
    }

    @Override
    public String getGetRequestParams() {
        if (mParams != null && mParams.size() >= 0) {
            StringBuilder builder = new StringBuilder("&");
            final Set<String> keys = mParams.keySet();
            for (String key : keys) {
                builder.append(key).append("=").append(mParams.get(key)).append("&");
            }
            builder.deleteCharAt(builder.length() - 1);
            return builder.toString();
        }
        return "";
    }

    @Override
    public String getUrl() {
        return UrlWrapper.GAME_CENTER_DOWNLOAD_STATICS_URL;
    }

    @Override
    public int getRequestType() {
        return RequestPackage.TYPE_GET;
    }

    @Override
    public void initParams(HashMap<String, String> params) {
        // 本地时间戳
        params.put("ctime", System.currentTimeMillis() / 1000l + "");
        // 平台：来源，0:未知，1:浏览器
        // 2:手机酷狗(Android版)，3:手机酷狗(Ios非越狱)，4:手机酷狗(Ios越狱)，5:游戏SDK(Android版)
        // 6:游戏SDK(Ios非越狱) 7:游戏SDK(Ios越狱)
        params.put("platform", "5");
        // 运营商
        params.put("spid", AppUtil.getMNCNum(mContext));
        // SDK版本
        params.put("clientversion", SDKControler.getSDKVersionCode() + "");
        // 系统版本
        params.put("sysversion", AppUtil.getSysVersionName());
        // 手机分辨率
        params.put("resolution", AppUtil.getScreenSizeStr(mContext));
        // 网络类型
        params.put("nettype", AppUtil.getNetworkType(mContext) + "");
        // 手机型号(去掉空格，url里不能有空格，如nexus 5)
        params.put("model", UrlEncodeUtil.encode(AppUtil.getPhoneModel()));
        // IMEI号
        params.put("imei", AppUtil.getMachineSN(mContext));
        // mid
        params.put("mid", mMid);
        // uuid
        params.put("uuid", mUuid);
        // gameid
        params.put("appid", mAppid + "");
        // 统计项POSITION。详细定义见接口文档
        params.put("positionid", mPositionid + "");
        // 统计项ITEM。详细定义见接口文档
        params.put("itemid", mItemid + "");
        // 统计类型。暂无用处，服务端保留字段
        params.put("stattype", "");
        // 统计值。如下载速度
        params.put("statvalue", mStatvalue);
        // 错误码
        params.put("errorcode", mErrorcode + "");
        // 错误信息
        params.put("errorlog", mErrorlog);
        // kugouid
        params.put("kugouid", mKugouid);
        // kugoutoken
        params.put("token", mKugoutoken);
        // appid
        params.put("clientAppid", mClientAppid + "");
        // statparams统计参数列表，服务端定义的标记
        params.put("statparams", UrlEncodeUtil.encode(mStatParams));

    }
}
